﻿Imports _1065144_1065164.DataProvider
Imports System.Data.SqlClient


Public Class KhachHangChoThueDAO
    Public Shared Function GetAll() As List(Of KhachHangChoThueDTO)
        Dim listKhachHangChoThue As New List(Of KhachHangChoThueDTO)
        Dim cn As SqlConnection = New SqlConnection(_strConnectionString)
        Dim cmd As New SqlCommand()
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "SelectChuNhaAll"
        cmd.Connection = cn
        cn.Open()

        Dim rd As SqlDataReader = cmd.ExecuteReader()
        While rd.Read
            Dim KhachHangChoThue As KhachHangChoThueDTO = New KhachHangChoThueDTO
            KhachHangChoThue.iMaKhachHangChoThue = rd.GetInt32(0)
            KhachHangChoThue.strHoTen = rd.GetString(1)
            KhachHangChoThue.strDiaChi = rd.GetString(2)
            KhachHangChoThue.strSoDienThoai = rd.GetString(3)
            listKhachHangChoThue.Add(KhachHangChoThue)

        End While
        cn.Close()
        Return listKhachHangChoThue
    End Function
    Public Shared Sub Insert(ByVal sqlparameters As SqlParameter())
        Dim cn As SqlConnection = New SqlConnection(_strConnectionString)
        Dim cmd As New SqlCommand()
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "khchothue"
        cmd.Parameters.AddRange(sqlparameters)

        cmd.Connection = cn
        cn.Open()
        cmd.ExecuteNonQuery()
        cn.Close()
    End Sub
    Public Shared Sub Update(ByVal sqlparameters As SqlParameter())
        Dim cn As SqlConnection = New SqlConnection(_strConnectionString)
        Dim cmd As New SqlCommand()
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "SuaKhachHangChoThue"
        cmd.Connection = cn
        cmd.Parameters.AddRange(sqlparameters)
        cn.Open()
        cmd.ExecuteNonQuery()
        cn.Close()
    End Sub
    Public Shared Sub Delete(ByVal sqlparameters As SqlParameter)
        Dim cn As SqlConnection = New SqlConnection(_strConnectionString)
        Dim cmd As New SqlCommand()
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "usp_XoaChuNha"
        cmd.Connection = cn
        cmd.Parameters.Add(sqlparameters)
        cn.Open()
        cmd.ExecuteNonQuery()
        cn.Close()
    End Sub


    Public Shared Function TimKiemTheoTen(ByVal sqlParameter As SqlParameter) As List(Of KhachHangChoThueDTO)
        Dim listKhachThue As New List(Of KhachHangChoThueDTO)
        Dim cn As SqlConnection = New SqlConnection(_strConnectionString)
        Dim cmd As New SqlCommand()
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "timchunhatheoten"
        cmd.Connection = cn
        If Not IsNothing(sqlParameter) Then
            cmd.Parameters.Add(sqlParameter)
        End If
        cn.Open()

        Dim rd As SqlDataReader = cmd.ExecuteReader()
        While rd.Read()
            Dim khachthue As KhachHangChoThueDTO = New KhachHangChoThueDTO()
            khachthue.iMaKhachHangChoThue = rd.GetInt32(0)
            khachthue.strHoTen = rd.GetString(1)
            khachthue.strDiaChi = rd.GetString(2)
            khachthue.strSoDienThoai = rd.GetString(3)
            listKhachThue.Add(khachthue)
        End While
        cn.Close()

        Return listKhachThue
    End Function

    Public Shared Function TimTheoMaChuNha(ByVal sqlParameter As SqlParameter) As List(Of KhachHangChoThueDTO)
        Dim listKhachThue As New List(Of KhachHangChoThueDTO)
        Dim cn As SqlConnection = New SqlConnection(_strConnectionString)
        Dim cmd As New SqlCommand()
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "timchunhatheoma"
        cmd.Connection = cn
        If Not IsNothing(sqlParameter) Then
            cmd.Parameters.Add(sqlParameter)
        End If
        cn.Open()

        Dim rd As SqlDataReader = cmd.ExecuteReader()
        While rd.Read()
            Dim khachthue As KhachHangChoThueDTO = New KhachHangChoThueDTO()
            khachthue.iMaKhachHangChoThue = rd.GetInt32(0)
            khachthue.strHoTen = rd.GetString(1)
            khachthue.strDiaChi = rd.GetString(2)
            khachthue.strSoDienThoai = rd.GetString(3)
            listKhachThue.Add(khachthue)
        End While
        cn.Close()

        Return listKhachThue
    End Function
End Class
